<template>
  <div>
    <div class="content">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        label-position="top"
        :rules="rules"
        label-width="80px"
        class="demo-ruleForm"
      >
        <div class="chooseMiniapp">
          <div class="transfer rowFlex columnCenter">
            <div class="transferItem">
              <div class="subTitle rowFlex columnCenter">
                <el-checkbox
                  v-model="checkAll"
                  :indeterminate="isIndeterminate"
                  @change="handleCheckAllChange"
                >
                  <span>选取小程序</span>
                </el-checkbox>
              </div>
              <div class="transferContent">
                <el-input
                  v-model="miniapp"
                  clearable
                  size="mini"
                  placeholder="请输入小程序名称/原始ID/AppId"
                  suffix-icon="el-icon-search"
                  style="margin-top:20px;"
                  @change="search"
                >
                </el-input>
                <el-checkbox-group
                  v-model="checkAppList"
                  class="columnFlex"
                  @change="handleCheckedCitiesChange"
                >
                  <el-checkbox
                    v-for="(item,index) in allCheckList"
                    :key="index"
                    style="margin-top:20px;"
                    :label="item"
                  >
                    {{ item.name }}
                    <svg-icon
                      v-if="item.app_type==1"
                      icon-class="miniapp"
                      class="imageIcon"
                    />
                    <svg-icon
                      v-else
                      icon-class="douyin"
                      class="imageIcon"
                    />
                  </el-checkbox>
                </el-checkbox-group>
              </div>
            </div>
            <div
              class="goRight rowFlex allCenter"
              @click="goRightContent"
            ><i class="el-icon-arrow-right"></i></div>
            <div class="transferItem">
              <div class="subTitle rowFlex columnCenter">
                <span>同步列表</span>
              </div>
              <div class="transferContent">
                <div
                  v-for="(item,index) in rightList"
                  :key="index"
                  class="rowFlex columnCenter spaceBetween"
                >
                  <p class="deleteItem">{{ item.name }}</p>
                  <i
                    class="el-icon-delete icon"
                    @click="removeItem(item,index)"
                  ></i>
                </div>
              </div>
            </div>
          </div>
        </div>
        <el-form-item
          label="定价"
          prop="pay_coin"
          style="margin-top:20px;"
          label-width="120px"
        >
          <div class="payinput rowFlex">
            <el-input
              v-model="ruleForm.pay_coin"
              clearable
              oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"
              style="width:250px;"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item
          label="开始付费剧集"
          style="margin-bottom:100px;"
          prop="start_chapter_num"
          class="chapter"
          label-width="120px"
        >
          <el-select
            v-model="ruleForm.start_chapter_num"
            clearable
            placeholder="请选择开始付费剧集"
            style="width:250px;"
          >
            <el-option
              v-for="item in chapterList"
              :key="item.id"
              :label="item.value"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer rowFlex">
      <div class="btns">
        <el-button
          :loading="loading"
          class="downVideo"
          type="primary"
          @click="offlinePlayLet"
        >一键同步</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { syncPlaylet } from '@/api/content'
import { miniAppList } from '@/api/common'
import { commonData } from '@/api/common'
import { validateCoin, validateStart } from '@/utils/index'
export default {
  computed: {
    ...mapState('user', ['miniAppList', 'mini_app_id'])
  },
  props: ['rowInfo'],
  data() {
    return {
      loading: false,
      miniapp: '',
      chapterList: [],
      checkAll: false,
      checkAppList: [],
      allCheckList: [],
      rightList: [],
      isIndeterminate: false,
      ruleForm: {
        pay_coin: '',
        start_chapter_num: ''
      },
      rules: {
        pay_coin: [
          { required: true, message: '请输入付费定价', trigger: 'change' },
          { required: true, validator: validateCoin, trigger: 'change' }
        ],
        start_chapter_num: [
          { required: true, message: '请选择付费集数', trigger: 'change' },
          { required: true, validator: validateStart, trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.search()
    this.reqeustChapter(this.rowInfo.id)
  },
  methods: {
    ...mapActions('user', ['requestMiniApp']),
    handleCheckAllChange(val) {
      console.log(val)
      val ? (this.checkAppList = [...this.allCheckList]) : (this.checkAppList = [])
      this.isIndeterminate = false
    },
    reqeustChapter(value) {
      commonData({ type: 'playletChapter', value: value }).then((res) => {
        if (res.status_code === 1) {
          this.chapterList = res.data.data
        }
      })
    },
    search() {
      const data = {
        key_word: this.miniapp
      }
      miniAppList(data).then((res) => {
        const list = res.data.filter((v) => this.rightList.every((val) => val.id !== v.id))
        this.allCheckList = JSON.parse(JSON.stringify(list))
      })
    },
    handlePlaylet() {
      // // 找出微信小程序 微信小程序中，只能上架审核通过的合同剧或版本剧（版本剧关联的合同剧必须为审核通过状态）
      // const wx = this.rightList.find(item => Number(item.app_type) == 1) // 找出微信小程序
      // if (wx && this.rowInfo.check_status != 2) {
      //   this.$message.warning('微信小程序中，只能上架审核通过的合同剧或版本剧（版本剧关联的合同剧必须为审核通过状态)')
      //   return false
      // } else {
      //   return true
      // }
      return true
    },
    offlinePlayLet() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.rightList.length === 0) {
            this.$message.warning('请选取小程序')
            return
          }
          if (Number(this.ruleForm.start_chapter_num) <= 0) {
            this.$message.warning('请选择开始付费剧集')
            return
          }
          if (this.handlePlaylet()) {
            this.submitData()
          }
        } else {
        }
      })
    },
    submitData() {
      this.loading = true
      const target_config = this.rightList.map((item) => {
        return {
          mini_app_id: item.id,
          up_status: 1,
          pay_coin: this.ruleForm.pay_coin,
          start_chapter_num: this.ruleForm.start_chapter_num
        }
      })
      const data = {
        playlet_ids: this.rowInfo.id,
        target_config
      }
      syncPlaylet(data).then((res) => {
        if (res.status_code === 1) {
          this.$message.success(res.msg)
          this.close()
        }
      })
      this.loading = false
    },
    goRightContent() {
      this.rightList = this.rightList.concat(this.checkAppList)
      const list = this.allCheckList.filter((v) => this.checkAppList.every((val) => val.id !== v.id))
      this.allCheckList = list
      this.checkAppList = []
    },
    handleCheckedCitiesChange(value) {
      const checkedCount = value.length
      this.checkAll = this.checkAppList.length === this.miniAppList.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.miniAppList.length
    },
    removeItem(item, index) {
      this.rightList.splice(index, 1)
      this.allCheckList.push(item)
    },
    close() {
      this.$emit('cancel')
    },
    submit() {
      this.$emit('ok')
    }
  }
}
</script>
  <style lang="scss" scoped>
.content {
  width: 686px;
  padding: 20px;
  ::v-deep .el-form-item__label {
    font-weight: 600;
  }
  .chooseMiniapp {
  }

  .goRight {
    width: 28px;
    height: 28px;
    background: #06c261;
    border-radius: 4px;
    color: #ffffff;
    font-size: 16px;
    margin: 0 20px;
    cursor: pointer;
  }
  .title {
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
    margin-bottom: 10px;
  }
  .miniapp {
  }
  .transfer {
    .transferItem {
      width: 290px;
      height: 482px;
      border-radius: 4px;
      border: 1px solid #d9d9d9;
      .subTitle {
        width: 288px;
        height: 40px;
        background: #f5f7fa;
        border-radius: 4px 4px 0px 0px;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 500;
        color: rgba(0, 0, 0, 0.88);
        padding: 0 20px;
        line-height: 40px;
        span {
          font-weight: 500;
          font-size: 14px;
        }
      }
      .transferContent {
        width: 100%;
        height: calc(100% - 50px);
        overflow: auto;
        padding: 0 20px;
        .deleteItem {
          font-size: 14px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: rgba(0, 0, 0, 0.88);
          margin: 10px 0;
        }
        .icon {
          cursor: pointer;
          color: #d9d9d9;
        }
      }
    }
    .transferLeft {
    }
    .transferRight {
    }
  }
}
.downVideo {
}
</style>